package com.xcdh.trading.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcdh.trading.entity.CapitalFlow;
import com.xcdh.trading.mapper.CapitalFlowMapper;
import com.xcdh.trading.response.TradingRes;
import com.xcdh.trading.service.CapitalFlowService;
import com.xcdh.trading.util.TuShareApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CapitalFlowServiceImpl extends ServiceImpl<CapitalFlowMapper, CapitalFlow> implements CapitalFlowService {
    @Autowired
    private CapitalFlowMapper capitalFlowMapper ;
    @Override
    public void syncCapitalFlow(String tradeDate) {
        TradingRes tradingRes = TuShareApi.post("moneyflow_dc", tradeDate);
        List<CapitalFlow> flowList = tradingRes.parse(CapitalFlow.class);
        List<CapitalFlow> list = lambdaQuery().eq(CapitalFlow::getTradeDate, tradeDate).list();
        Map<String, CapitalFlow> flowMap = list.stream().collect(Collectors.toMap(CapitalFlow::getTsCode, Function.identity()));
        for (CapitalFlow capitalFlow : flowList) {
            CapitalFlow flow = flowMap.get(capitalFlow.getTsCode());
            if(flow == null){
                save(capitalFlow) ;
            }
        }
    }

    @Override
    public List<CapitalFlow> statisticsBuyMoney(String tradeDate) {
        return capitalFlowMapper.statisticsBuyMoney(tradeDate);
    }
    // 可实现自定义业务逻辑方法
}
